/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SingleMessageDialog.java
 *
 * Created on 24 Sep, 2011, 4:32:37 PM
 */

package net.sf.sbv.ui;

import java.awt.Color;
import net.sf.sbv.modals.Message;
import net.sf.sbv.modals.messageUI.MessageTheme;

/**
 *
 * @author Pratik
 */
public class SingleMessageDialog extends javax.swing.JDialog {
    private Message message;

    /** Creates new form SingleMessageDialog */
    public SingleMessageDialog(java.awt.Frame parent, Message message) {
        super(parent, true);
        this.message = message;
        initComponents();
        this.setLocationRelativeTo(parent);

        updateData();
    }

    public static void showMessageDialog(java.awt.Frame parent, Message message) {
        new SingleMessageDialog(parent, message).setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contactPanel = new javax.swing.JPanel();
        innerContactPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lbMsgType = new javax.swing.JLabel();
        lbContactDetails = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        lbMessageLength = new javax.swing.JLabel();
        cmdPreviousMsg = new javax.swing.JButton();
        cmdNextMsg = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        lbMessageNumber = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        contactPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("From:");

        lbMsgType.setText("Type");

        lbContactDetails.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbContactDetails.setText("None");

        javax.swing.GroupLayout innerContactPanelLayout = new javax.swing.GroupLayout(innerContactPanel);
        innerContactPanel.setLayout(innerContactPanelLayout);
        innerContactPanelLayout.setHorizontalGroup(
            innerContactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(innerContactPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(innerContactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(innerContactPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbContactDetails, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE))
                    .addComponent(lbMsgType))
                .addContainerGap())
        );
        innerContactPanelLayout.setVerticalGroup(
            innerContactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(innerContactPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(innerContactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbContactDetails))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbMsgType)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        contactPanel.add(innerContactPanel, java.awt.BorderLayout.CENTER);
        contactPanel.add(jSeparator1, java.awt.BorderLayout.SOUTH);

        jTextArea1.setColumns(20);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Copy All");
        jButton2.setToolTipText("Copy message to clipboard");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        lbMessageLength.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbMessageLength.setText("Length of Message");

        cmdPreviousMsg.setText("<<");
        cmdPreviousMsg.setToolTipText("Open previous message");
        cmdPreviousMsg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPreviousMsgActionPerformed(evt);
            }
        });

        cmdNextMsg.setText(">>");
        cmdNextMsg.setToolTipText("Open next message");
        cmdNextMsg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNextMsgActionPerformed(evt);
            }
        });

        jLabel2.setText("Message #:");

        lbMessageNumber.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbMessageNumber)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 202, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
            .addComponent(contactPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmdPreviousMsg)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdNextMsg)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbMessageLength, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(contactPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdPreviousMsg)
                    .addComponent(cmdNextMsg)
                    .addComponent(lbMessageLength))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jLabel2)
                    .addComponent(lbMessageNumber))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.jTextArea1.selectAll();
        this.jTextArea1.copy();
        jTextArea1.setCaretPosition(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void cmdPreviousMsgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPreviousMsgActionPerformed
        traverseMessages(-1);
    }//GEN-LAST:event_cmdPreviousMsgActionPerformed

    private void cmdNextMsgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNextMsgActionPerformed
        traverseMessages(1);
    }//GEN-LAST:event_cmdNextMsgActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdNextMsg;
    private javax.swing.JButton cmdPreviousMsg;
    private javax.swing.JPanel contactPanel;
    private javax.swing.JPanel innerContactPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lbContactDetails;
    private javax.swing.JLabel lbMessageLength;
    private javax.swing.JLabel lbMessageNumber;
    private javax.swing.JLabel lbMsgType;
    // End of variables declaration//GEN-END:variables

    private void updateData() {
        jTextArea1.setText(message.getMessageText());
        jTextArea1.setCaretPosition(0);
        this.lbContactDetails.setText(message.getContact().toString());
        this.setTitle(message.getContact().getName());
        this.lbMsgType.setText(message.getTimeStamp());
        this.lbMessageNumber.setText(message.getContact().getMessages().indexOf(message) + 1 + "");
        
        Color bckgrnd = null;
        if(message.getMessageType() == Message.RECEIVED) {
            bckgrnd = MessageTheme.getDefaultTheme().getReceivedMessageColor();
        } else {
            bckgrnd = MessageTheme.getDefaultTheme().getSentMessageColor();
        }
        innerContactPanel.setBackground(bckgrnd);
        
        updateLengthOfMessage();

        enableDisablePreviousNext();
    }

    private void updateLengthOfMessage() {
        //update the length of the message
        int length = jTextArea1.getText().length();
        
        //character length
        String characterLength = length + " Character";
        if(length > 1) {
            characterLength += "s";
        }
        characterLength += ". ";

        //message count
        int messageCount = (int)((float)length / 160f) + (length % 160 == 0 ? 0 : 1);
        String messageCountStr = messageCount + " Message";

        if(messageCount > 1) {
            //make it plural
            messageCountStr += "s";
        }
        messageCountStr += ".";
        lbMessageLength.setText(characterLength + messageCountStr);
    }

    private void enableDisablePreviousNext() {
        int indexOfCurrentMessage = message.getContact().getMessages().indexOf(message);

        boolean enablePrevious = indexOfCurrentMessage > 0;
        boolean enableNext = indexOfCurrentMessage < (message.getContact().getMessages().size() - 1);

        cmdPreviousMsg.setEnabled(enablePrevious);
        cmdNextMsg.setEnabled(enableNext);
    }

    private void traverseMessages(int offset) {
        int indexOfCurrentMessage = message.getContact().getMessages().indexOf(message);
        message = message.getContact().getMessages().get(indexOfCurrentMessage + offset);
        updateData();
    }

}
